//封装购物车模块
import { computed, ref } from "vue";
import { defineStore } from "pinia";
import { useUserStore } from "./user";
import { insertCartAPI, findNewCartListAPI, delCartAPI } from "@/apis/cart";
import { ElMessage } from "element-plus";
import { changeCartAPI, cartAllselectedAPI } from "@/apis/cart";

export const useCartStore = defineStore('cart', () => {
  const userStore = useUserStore()
  //1.定义state
  const cartList = ref([])
  //2.定义action
  const addCart = async (goods) => {
    const isLogin = userStore.userInfo.token
    if (isLogin) {
      //登陆后的加入购物车逻辑
      await insertCartAPI(goods.skuId, goods.count)
      updateNewList()
      ElMessage.success('加入购物车成功')
    } else {
      //添加购物车操作
      //已添加过 - count + 1
      //没有添加过 - push
      //思路：通过匹配传递过来的商品对象中的skuId能否在cartList找到
      const item = cartList.value.find((item) => goods.skuId === item.skuId)
      if (item) {
        //找到了
        item.count++
      } else {
        cartList.value.push(goods)
      }
      ElMessage.success('加入本地购物车成功')
    }
  }

  //删除购物车操作
  const delCart = async (skuId) => {
    const isLogin = userStore.userInfo.token
    if (isLogin) {
      //调用接口实现接口购物车删除功能
      await delCartAPI([skuId])
      updateNewList()
    } else {
      //思路：1.找到要删除项的下标值 - splice； 2.使用数组过滤方法 - filter
      const index = cartList.value.findIndex((item) => skuId === item.skuId)
      cartList.value.splice(index, 1)
    }
  }

  //封装获取最新购物车列表的action
  const updateNewList = async () => {
    const res = await findNewCartListAPI()
    cartList.value = res.result
  }


  //清除购物车
  const clearCart = () => {
    cartList.value = []
  }

  //单选功能
  const singleCheck = (skuId) => {
    const isLogin = userStore.userInfo.token
    //通过skuId找到要修改的那一项
    const item = cartList.value.find((item) => {
      return item.skuId === skuId
    })
    item.selected = !item.selected
    if (isLogin) {
      changeCartAPI(item.skuId, item.selected, item.count)
    }
  }


  //全选功能
  const allCheck = (selected) => {
    const isLogin = userStore.userInfo.token
    //将cartList中的每一项的selected都设置为isAll的状态
    cartList.value.forEach(item => item.selected = selected)
    if (isLogin) {
      cartAllselectedAPI(selected, cartList.value.map((item) => {
        return item.skuId
      }))
    }
  }

  //计算属性
  //1.总数量，count之和
  const allCount = computed(() => {
    return cartList.value.reduce((a, c) => a + c.count, 0)
  })

  //2.总价，所有项的count*price之和
  const allPrice = computed(() => {
    return cartList.value.reduce((a, c) => a + c.count * c.price, 0)
  })

  //是否全选
  const isAll = computed(() => {
    return cartList.value.every((item) => item.selected === true)
  })

  //已选择数量
  const selectedCount = computed(() => cartList.value.filter(item => item.selected === true).reduce((a, c) => a + c.count, 0))
  //已选择商品总价
  const selectedPrice = computed(() => cartList.value.filter(item => item.selected === true).reduce((a, c) => a + c.count * c.price, 0))
  //已选择商品总价
  return {
    selectedCount,
    selectedPrice,
    cartList,
    isAll,
    addCart,
    delCart,
    allCount,
    allPrice,
    singleCheck,
    allCheck,
    clearCart,
    updateNewList
  }
}, {
  persist: true
})
